import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_widgets/resources/l10n/kq_intl.dart';

class EllipsisText extends StatefulWidget {
  final String text;
  final int maxLines;
  final double? fontSize;
  final Color? color;
  const EllipsisText(
      {super.key,
      required this.text,
      this.maxLines = 1,
      this.fontSize,
      this.color});

  @override
  State<EllipsisText> createState() => _EllipsisTextState();
}

class _EllipsisTextState extends State<EllipsisText> {
  bool isExpand = false;
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        if (!isExpand) {
          double charH = 0;
          int heightChangeNum = 0;
          String result = '';

          /// 最后一行的数据
          String secStr = '';

          /// 最后一行第一个字符的位置
          int index = 0;
          for (var i = 1; i < widget.text.length; i++) {
            TextPainter painter = TextPainter(
                maxLines: widget.maxLines + 1,
                textDirection: TextDirection.ltr,
                text: TextSpan(
                    text: widget.text.substring(0, i),
                    style: TextStyle(fontSize: widget.fontSize)))
              ..layout(
                maxWidth: constraints.maxWidth,
              );

            /// 英文和中文的高度不一样，不能单纯的判断高度
            if (charH + 8 <= painter.size.height) {
              charH = painter.size.height;
              heightChangeNum++;
              if (heightChangeNum == widget.maxLines && index == 0) {
                index = i - 1;
              }
            }
            if (heightChangeNum == widget.maxLines) {
              secStr = widget.text.substring(index, i);
            }
            if (heightChangeNum > widget.maxLines) {
              result = widget.text.substring(0, i);
              break;
            }
          }
          String res = '';
          double charHeight = 0;
          for (var j = 0; j < secStr.length; j++) {
            TextPainter painterC = TextPainter(
                maxLines: 2,
                textDirection: TextDirection.ltr,
                text: TextSpan(
                    text:
                        '${secStr.substring(0, j)}...${KqIntl.currentResource.more}',
                    style: TextStyle(fontSize: widget.fontSize)))
              ..layout(
                maxWidth: constraints.maxWidth,
              );
            if (charHeight + 8 <= painterC.size.height && charHeight == 0) {
              charHeight = painterC.size.height;
            }
            if (painterC.size.height > charHeight) {
              res = result
                  .substring(0, result.length - 1)
                  .replaceAll(secStr, '${secStr.substring(0, j - 1)}...');
              break;
            }
          }
          InlineSpan span = TextSpan(children: [
            TextSpan(
              text: res,
              style: TextStyle(
                fontSize: widget.fontSize,
                color: KqThemeColors.text59,
              ),
            ),
            TextSpan(
              text: KqIntl.currentResource.more,
              style: TextStyle(
                  fontSize: widget.fontSize, color: KqThemeColors.textBlue),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  // 处理更多按钮点击事件
                  setState(() {
                    isExpand = true;
                  });
                },
            ),
          ]);
          return RichText(
            text: span,
          );
        } else {
          InlineSpan span = TextSpan(children: [
            TextSpan(
              text: widget.text,
              style: TextStyle(
                fontSize: widget.fontSize,
                color: KqThemeColors.text59,
              ),
            ),
            TextSpan(
              text: KqIntl.currentResource.collapse,
              style: TextStyle(
                  fontSize: widget.fontSize, color: KqThemeColors.textBlue),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  // 处理收起按钮点击事件
                  setState(() {
                    isExpand = false;
                  });
                },
            ),
          ]);
          return RichText(
            text: span,
          );
        }
      },
    );
  }
}
